import networkx as nx
import numpy as np
import glob
import os, os.path
import math
import pickle
import scipy.sparse
import random
from scipy.sparse.linalg import svds
from sklearn.decomposition import PCA
import pandas as pd
def getS_rpr():
    return 0

def getCM(A):
    return A*A

def getAA(A):
    return A*A

def getKatz(A,beta):
    return A


def getProbabilityMatrix(adj,method):
    adj_ = adj.toarray()
    if method=='common neighbors':
        S = adj.dot(adj)
    return S.toarray()

def svd_probabilityMatrix(S,components):
    S = S.asfptype()
    u, s, vt = svds(S,k=components)
    return u,s,vt

def pca_probabilityMatrix(M,components):
   pca = PCA(n_components=components)
   pca.fit(M)
   newM = pca.fit_transform(M)
   variance_ratio_ = pca.explained_variance_ratio_
   print(pca.explained_variance_ratio_)
   print(np.sum(variance_ratio_))
   return newM


prefix_name = 'D:/data/similarity_matrix/Hope/'
def save_Smatrix_newS(adj,dela_name):
   components = 16
   method = 'common neighbors'
   S = getProbabilityMatrix(adj,method)
   newS = pca_probabilityMatrix(S, components)
   ori_Smatirx_name = dela_name + '-ori.pkl'
   pca_Smatirx_name = dela_name + '-pca-{}.pkl'.format(components)
   file_name = prefix_name + ori_Smatirx_name
   with open(file_name, 'wb') as f:
      pickle.dump(S, f, protocol=2)
   print(file_name+"S orimatrix saved!")

   file_name = prefix_name + pca_Smatirx_name
   with open(file_name, 'wb') as f:
      pickle.dump(newS, f, protocol=2)
   print(file_name+"S pcamatrix saved!")

if __name__ == '__main__':
    adjs = []
    # for dela_name in DEAL_NAME:
    #     combined_dir = 'D:/data-processed/{}-adj.pkl'.format(dela_name)
    #     with open(combined_dir, 'rb') as f:
    #         adj = pickle.load(f)
    #     adjs.append(adj)
    #     # save_Smatrix_newS(adj,dela_name)
    # print('')
    # df = pd.read_csv('PB-ori-All.csv')
    wf = pd.DataFrame()
    df = pd.read_csv('PB-ori-All.csv')
    df = pd.concat([df,wf],axis=0)

    print(df.values[1][1:])